import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_trip_demo/model/sales_box_model.dart';
import 'package:flutter_trip_demo/utils/navigator/navigator_util.dart';

import '../model/common_model.dart';
import 'my_webview.dart';

///底部卡片入口
class ActBox extends StatelessWidget {
  final SalesBoxModel? salesBox;

  /*
  * 下面两层导航栏控件
  * */
  const ActBox({Key? key, required this.salesBox}) : super(key: key);

  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(
        color: Colors.white,
      ),
      child:_items(context),
    );
  }

  _items(BuildContext context) {
    if (salesBox == null) {
      return null;
    }
    List<Widget> items = [];
    items.add(_doubleItem(context,salesBox!.bigCard1,salesBox!.bigCard2,true,false));
    items.add(_doubleItem(context,salesBox!.smallCard1,salesBox!.smallCard2,false,false));
    items.add(_doubleItem(context,salesBox!.smallCard3,salesBox!.smallCard4,false,true));
    /*
    * 计算出第一行显示的数量
    * */

    return Column(
      children: <Widget>[
        Container(
          height: 44,
          margin: const EdgeInsets.only(left: 10),
          decoration: const BoxDecoration(
            border: Border(bottom: BorderSide(width: 1,color: Color(0xff2f2f2))),
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Image.network(salesBox!.icon,
                height: 15,fit: BoxFit.fill,),
              Container(
                padding: const EdgeInsets.fromLTRB(10, 1, 8, 1),
                margin: const EdgeInsets.only(right: 7),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(12),
                  gradient: const LinearGradient(colors: [
                    Color(0xffff4e63),Color(0xffff6cc9),
                  ],begin: Alignment.centerLeft,end: Alignment.centerRight),),
                child: GestureDetector(
                  onTap: (){
                    NavigatorUtil.push(
                      context,
                      MyWebView(
                        url: salesBox!.moreUrl,
                        title: '更多活动',
                      )
                    );
                  },
                  child:const Text('获取更多福利>',style:  TextStyle(color: Colors.white,fontSize: 12),),
                ),
              )
            ],
          ),
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children:
            items.sublist(0,1),
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children:
          items.sublist(1,2),
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children:
          items.sublist(2,3),
        )
      ],
    );
  }

  _doubleItem(BuildContext context,CommonModel left,CommonModel right,bool max,bool end){
     return Row(
       mainAxisAlignment: MainAxisAlignment.spaceAround,
       children: [
         _item(context, left,max,true,end),
         _item(context, right,max,false, end),
       ],
     );
  }

  Widget _item(BuildContext context, CommonModel model,bool max,bool left,bool end) {
    BorderSide borderSide = const BorderSide(width: 0.8,color: Color(0xff2f2f2));
    return GestureDetector(
          onTap: () {
            NavigatorUtil.push(
              context,
              MyWebView(
                url: model.url,
                statusBarColor: model.statusBarColor,
                hideAppBar: model.hideAppBar,
              )
            );
          },
          child: Container(
            decoration: BoxDecoration(
              border:Border(right: left?borderSide:BorderSide.none,
              bottom: end?BorderSide.none:borderSide)
            ),
            child: Image.network(
              model.icon,
              fit: BoxFit.fill,
              width: MediaQuery.of(context).size.width/2-10,
              height: max?129:80,
            ),
          )
        );
  }
}
